package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.ClassifyDetailsModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/3/11.
 */

public class ClassifyDetailsAdapter extends RecyclerView.Adapter<ClassifyDetailsAdapter.Holder> {

    private Context context;
    private List<ClassifyDetailsModel> dataList;

    public ClassifyDetailsAdapter(List<ClassifyDetailsModel> dataList) {
        this.dataList = new ArrayList<>();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
    }

    public void refresh(List<ClassifyDetailsModel> dataList) {
        this.dataList.clear();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    public void add(List<ClassifyDetailsModel> dataList) {
        if (dataList != null) {
            int start = this.dataList.size();
            this.dataList.addAll(dataList);
            notifyItemRangeInserted(start, dataList.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_classify_details, parent, false);

        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final ClassifyDetailsModel model = dataList.get(position);
        holder.name.setText(model.getName());

        Glide.with(context)
                .load(model.getImage())
                .error(R.drawable.im_default)
                .into(holder.image);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClicked(model);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.name)
        TextView name;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnClassifyItemClickedListener listener;

    public void setOnClassifyItemClickedListener(OnClassifyItemClickedListener listener) {
        this.listener = listener;
    }

    public interface OnClassifyItemClickedListener {
        void onItemClicked(ClassifyDetailsModel model);
    }
}
